"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
window.onload = () => {
    var br = document.createElement("br");
    document.body.appendChild(br);
    var searchParams = new URLSearchParams(window.location.search);
    var name = searchParams.get('_localext_');
    var span = document.createElement("span");
    span.textContent = "ext port=" + name;
    document.body.appendChild(span);
    if (name != null) {
        let port = parseInt(name);
        localexturl = "http://127.0.0.1:" + port;
        testLocalExt(port);
    }
};
var localexturl = "";
function testLocalExt(port) {
    return __awaiter(this, void 0, void 0, function* () {
        var br = document.createElement("br");
        document.body.appendChild(br);
        var info = yield fetch("http://127.0.0.1:" + port + "/info");
        var result = JSON.parse(yield info.text());
        var span = document.createElement("span");
        span.textContent = JSON.stringify(result);
        document.body.appendChild(span);
        var br = document.createElement("br");
        document.body.appendChild(br);
        var d = yield fetch("http://127.0.0.1:" + port + "/setdebug?value=false");
        var span = document.createElement("span");
        span.textContent = yield d.text();
        ;
        document.body.appendChild(span);
        var br = document.createElement("br");
        document.body.appendChild(br);
        var btn = document.createElement("button");
        document.body.appendChild(btn);
        btn.textContent = "test dir";
        btn.onclick = test_dir;
        var br = document.createElement("br");
        document.body.appendChild(br);
        var btn = document.createElement("button");
        document.body.appendChild(btn);
        btn.textContent = "test file";
        btn.onclick = test_file;
        var br = document.createElement("br");
        document.body.appendChild(br);
        var btn = document.createElement("button");
        document.body.appendChild(btn);
        btn.textContent = "test Quit";
        btn.onclick = test_quit;
    });
}
function test_dir() {
    return __awaiter(this, void 0, void 0, function* () {
        var info = yield fetch(localexturl + "/fileext/getroots");
        var txt = yield info.text();
        var json = JSON.parse(txt);
        var mydoc = json["special"]["MyDocuments"];
        addLine(txt);
        var info = yield fetch(localexturl + "/fileext/dir_info" + "?path=" + mydoc);
        var txt = yield info.text();
        addLine(txt);
        var info = yield fetch(localexturl + "/fileext/dir_list" + "?path=" + "D:\\git\\condroid" + "&pattern=*.jpg" + "&recursion=true");
        var txt = yield info.text();
        addLine(txt);
        var info = yield fetch(localexturl + "/fileext/dir_create" + "?path=" + "D:\\git\\__001");
        var txt = yield info.text();
        addLine(txt);
        var info = yield fetch(localexturl + "/fileext/dir_create" + "?path=" + "D:\\git\\__002");
        var txt = yield info.text();
        addLine(txt);
        var info = yield fetch(localexturl + "/fileext/dir_delete" + "?path=" + "D:\\git\\__002");
        var txt = yield info.text();
        addLine(txt);
        var info = yield fetch(localexturl + "/fileext/dir_create" + "?path=" + "D:\\git\\__003");
        var txt = yield info.text();
        addLine(txt);
    });
}
function test_quit() {
    return __awaiter(this, void 0, void 0, function* () {
        var info = yield fetch(localexturl + "/quit");
        var txt = yield info.text();
        addLine(txt);
    });
}
function test_file() {
    return __awaiter(this, void 0, void 0, function* () {
        var info = yield fetch(localexturl + "/fileext/dir_list" + "?path=" + "D:\\git\\condroid" + "&pattern=*.jpg" + "&recursion=true");
        var txt = yield info.text();
        var json = JSON.parse(txt);
        var file = json["files"][0];
        addLine("==find file=" + file);
        var info = yield fetch(localexturl + "/fileext/file_info" + "?path=" + file);
        var txt = yield info.text();
        addLine("==file_info file=" + txt);
        var info = yield fetch(localexturl + "/fileext/file_info" + "?path=" + "d:\\ttt.jpg");
        var txt = yield info.text();
        addLine("==file_info ttt file=" + txt);
        //test delete
        var info = yield fetch(localexturl + "/fileext/file_delete" + "?path=" + "d:\\ttt.jpg");
        var txt = yield info.text();
        addLine("==file_delete ttt file=" + txt);
        var info = yield fetch(localexturl + "/fileext/file_info" + "?path=" + "d:\\ttt.jpg");
        var txt = yield info.text();
        addLine("==file_info ttt file=" + txt);
        //test copy
        var info = yield fetch(localexturl + "/fileext/file_copy" + "?from=" + file + "&to=" + "d:\\ttt.jpg");
        var txt = yield info.text();
        addLine("==file_copy=" + txt);
        var info = yield fetch(localexturl + "/fileext/file_info" + "?path=" + "d:\\ttt.jpg");
        var txt = yield info.text();
        addLine("==file_info ttt file=" + txt);
        //test read
        var re = yield fetch(localexturl + "/fileext/file_read" + "?path=" + file + "&format=binary");
        if (re.status != 200) {
            addLine("re=" + re.statusText);
        }
        var f = yield re.arrayBuffer();
        addLine("load file len=" + f.byteLength);
        //test write
        var reqop = { "method": "post", "body": f };
        var info = yield fetch(localexturl + "/fileext/file_write" + "?path=" + "d:\\ttt2.jpg", reqop);
        var txt = yield info.text();
        addLine("==file_write ttt2 file=" + txt);
        //const headers = new Headers();
        //headers.append("Content-Type", "text/plain; charset=utf-8"); // 指定编码为 utf-8
        var reqop = { "method": "post", "body": "very good.地球上的啥子" }; //, "headers": headers };
        var info = yield fetch(localexturl + "/fileext/file_write" + "?path=" + "d:\\ttt3.info", reqop);
        var txt = yield info.text();
        addLine("==file_write ttt3 file=" + txt);
        var formdata = new FormData();
        formdata.append("n1", new Blob(["hasdfd地球上"]), "f1.txt");
        var reqop = { "method": "post", "body": formdata };
        var info = yield fetch(localexturl + "/fileext/file_write" + "?path=" + "d:\\ttt4.info", reqop);
        var txt = yield info.text();
        addLine("==file_write ttt4 file=" + txt);
        //test read
        var re = yield fetch(localexturl + "/fileext/file_read" + "?path=" + "d:\\ttt4.info" + "&format=text");
        var text = yield re.text();
        addLine("code=" + re.status + "load text=" + text);
        //test read error
        var re = yield fetch(localexturl + "/fileext/file_read" + "?path=" + "d:\\ttt44.info" + "&format=text");
        var text = yield re.text();
        addLine("code=" + re.status + "load text=" + text);
    });
}
function addLine(txt) {
    var br = document.createElement("br");
    document.body.appendChild(br);
    var span = document.createElement("span");
    span.textContent = txt;
    document.body.appendChild(span);
}
